#include "stm32f10x.h"
#include <string.h>
#include "../../SYSTEM/flash/flash.h"
#include "../../SYSTEM/utils/utils.h"
#include "./dlt645.h"
#define MAX_STOP_RECORD_COUNT 2
// 停电记录处理
u8 Voltage_Zero_count = 0; // 电压0 计数
u32 Voltage_Zero_record_number = 0;
u8 is_in_stop = 0;

void stop_restore_record()
{
        // SPrint("*****restroe.....\r\n");
    Voltage_Zero_count = 0;
    is_in_stop = 0;
    dlt645_restroeProcess();
}

void stop_write_record()
{
    // 需要先读出来
}

void stop_save_record()
{
    // if voltage is zero  and count > 5 (5s)
    u8 current_time[6] = {0};
    u32 d[2] = {0};
    Voltage_Zero_count++;
    if (Voltage_Zero_count > MAX_STOP_RECORD_COUNT)
    {

        if (is_in_stop == 0)
        {
            SPrint("*****stop.....\r\n");

            getCurrentTime(current_time);
            WPrint("stop time: %02d-%02d-%02d %02d:%02d:%02d \r\n ", current_time[5], current_time[4], current_time[3],
                   current_time[2], current_time[1], current_time[0]);
            d[0] = (current_time[0]) + (current_time[1] << 8) + (current_time[2] << 16) + (current_time[3] << 24);
            d[1] = (current_time[4]) + (current_time[5] << 8);
            // 仅仅存放当前时间,100条记录循环写,由于写FLASH为4字节对齐，这儿采用8字节表示时间，空余补0

            if (Voltage_Zero_record_number == 0xffffffff)
            {
                // first time
                Voltage_Zero_record_number = 1;
            }
            else
            {
                Voltage_Zero_record_number++;
            }

            if (Voltage_Zero_record_number >= 0x1000000)
            {
                Voltage_Zero_record_number = 1;
            }
            // 这儿后续分散写
            WPrint("write flash: %d %x %x", Voltage_Zero_record_number, d[0], d[1]);

            WriteFlash(ENGERY_RECORD_NUMBER_ADDRESS, &Voltage_Zero_record_number, 1);
            WriteFlash(ZERO_VOLTAGE_RECORD_ADDRESS + 8 * ((Voltage_Zero_record_number - 1) % 128), d, 2);
            // 发送事件
            dlt645_sendStopEventMsg(false);
            is_in_stop = 1;
        }
    }
}

void stop_get_inital_value()
{

    ReadFlash(ENGERY_RECORD_NUMBER_ADDRESS, &Voltage_Zero_record_number, 1);
    WPrint("last stop record total: %d\r\n", Voltage_Zero_record_number);
}

// 6 字节 返回1 表示成功，0 表示没有呼叫
u8 stop_get_last_record(u8 *data)
{
    u32 d[2] = {0};
    if (Voltage_Zero_record_number == 0xffffffff)
    {
        memcpy(data, d, 6); // copy 6 byte
        return 0;
    }
    ReadFlash(ZERO_VOLTAGE_RECORD_ADDRESS + 8 * ((Voltage_Zero_record_number - 1) % 128), d, 2);
    WPrint("last record %x %x", d[1], d[0]);
    memcpy(data, &(d[0]), 4);
    memcpy(data + 4, &(d[1]), 2);
    return 1;
}

// 3字节 XX XX XX
void stop_get_total(u8 *data)
{
    u8 dz[3] = {0};
    if (Voltage_Zero_record_number != 0xffffffff)
    {

        memcpy(data, &Voltage_Zero_record_number, 3);
    }
    else
    {

        memcpy(data, dz, 3);
    }
}

void stop_check_timer_process()
{
    WPrint("#check:%d \r\n", is_in_stop);
    // if (is_in_stop == 0)
    // {
    //     return;
    // }
    // 在停电处理，检查是否需要重新发送
    dlt645_sendStopEventMsg(true);
}

void stop_process()
{
    // 保存记录
    stop_save_record();
    // 检查一下是否需要定时发送事件
    stop_check_timer_process();
}
